% CALCULATE 'AVERAGE' VELOCITY (from 0ms to start of 1st occlusion)
% for slow speed

% target18: occluder present target18.plotocc.x(1,1)ms to target18.plotocc.x(1,2)ms 
% & target18.plotocc.x(2,1)ms to target18.plotocc.x(2,2)ms

% equiv. (i.e. same part of phase) non-occluded period =  
% eqocc18ms - target18.plotocc.x(1,2)ms
eqocc18 = msPerhCycle - target18.plotocc.x(1,2);

% NB initial delay (200ms) in the occluder is omitted in calculating the 
% resid pred gain, therefore it must be omitted from the equivalent
% non-occluded period

SN.avgvel = mean(SN.onevelnosac(eqocc18:(target18.plotocc.x(1,1)-delay),:));
SS.avgvel = mean(SS.onevelnosac(eqocc18:(target18.plotocc.x(1,1)-delay),:));

target18.avgvel = mean(target18.Xvel(eqocc18:(target18.plotocc.x(1,1)-delay),:));

% CALCULATE CLOSED LOOP GAIN (mean eye velocity / mean target velocity)

SN.gain = SN.avgvel/target18.avgvel;
SS.gain = SS.avgvel/target18.avgvel;

% CALCULATE MEAN VELOCITY ERROR (for equiv path to occluded part)
% = mean of all the diffs between eye (w/out saccs) and target velocity

SN.velerr = mean(abs(target18.onevel(eqocc18:(target18.plotocc.x(1,1)-delay),:) - ...
    SN.onevelnosac(eqocc18:(target18.plotocc.x(1,1)-delay),:)));
SS.velerr = mean(abs(target18.onevel(eqocc18:(target18.plotocc.x(1,1)-delay),:) - ...
    SS.onevelnosac(eqocc18:(target18.plotocc.x(1,1)-delay),:)));

% CALCULATE MEAN RESIDUAL PREDICTIVE VELOCITIES (mean velocity behind occluder
% discounting an inital delay as eye velocity is maintained for this
% period)
               
% rpv(1) = rpv in 1st occlusion (without negative sign)

SN.rpv(1) = -mean(SN.onevelnosac((target18.plotocc.x(1,1)+delay):...
    target18.plotocc.x(1,2),:));
SS.rpv(1) = -mean(SS.onevelnosac((target18.plotocc.x(1,1)+delay):...
    target18.plotocc.x(1,2),:));

target18.avgoccvel(1) = -mean(target18.Xvel((target18.plotocc.x(1,1)...
    +delay):target18.plotocc.x(1,2),:));

% rpv(2) = rpv in 2nd occlusion

SN.rpv(2) = mean(SN.onevelnosac((target18.plotocc.x(2,1)+delay):...
    target18.plotocc.x(2,2),:));
SS.rpv(2) = mean(SS.onevelnosac((target18.plotocc.x(2,1)+delay):...
    target18.plotocc.x(2,2),:));

target18.avgoccvel(2) = mean(target18.Xvel((target18.plotocc.x(2,1)...
    +delay):target18.plotocc.x(2,2),:));

% CALCULATE RESIDUAL PREDICTIVE GAINS

SN.rpg(1) = SN.rpv(1)/target18.avgoccvel(1);
SS.rpg(1) = SS.rpv(1)/target18.avgoccvel(1);

SN.rpg(2) = SN.rpv(2)/target18.avgoccvel(2);
SS.rpg(2) = SS.rpv(2)/target18.avgoccvel(2);

% CALCULATE MEAN VELOCITY ERROR FOR OCCLUDED PARTS

% rve(1) = resid mean velocity error in 1st occlusion (decelerating)

SN.rve(1) = mean(abs(target18.onevel((target18.plotocc.x(1,1)+delay):target18.plotocc.x(1,2),:) - ...
    SN.onevelnosac((target18.plotocc.x(1,1)+delay):target18.plotocc.x(1,2),:)));
SS.rve(1) = mean(abs(target18.onevel((target18.plotocc.x(1,1)+delay):target18.plotocc.x(1,2),:) - ...
    SS.onevelnosac((target18.plotocc.x(1,1)+delay):target18.plotocc.x(1,2),:)));

% rve(2) = resid mean velocity error in 2nd occlusion (accelerating)

SN.rve(2) = mean(abs(target18.onevel((target18.plotocc.x(2,1)+delay):target18.plotocc.x(2,2),:) - ...
    SN.onevelnosac((target18.plotocc.x(2,1)+delay):target18.plotocc.x(2,2),:)));
SS.rve(2) = mean(abs(target18.onevel((target18.plotocc.x(2,1)+delay):target18.plotocc.x(2,2),:) - ...
    SS.onevelnosac((target18.plotocc.x(2,1)+delay):target18.plotocc.x(2,2),:)));

% PUT GAINS & VELOCITY ERRORS IN PLOTABLE FORM

Stats.gain(:,1) = [SN.gain, SS.gain];
Stats.gain(:,2) = [SN.rpg(2), SS.rpg(2)];
Stats.gain(:,3) = [SN.rpg(1), SS.rpg(1)];

Stats.velerr(:,1) = [SN.velerr, SS.velerr];
Stats.velerr(:,2) = [SN.rve(2), SS.rve(2)];
Stats.velerr(:,3) = [SN.rve(1), SS.rve(1)];